package com.feicheng.house.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.house.entity.VipClientDepositCardVo;
import com.feicheng.house.mapper.VipClientDepositCardVoMapper;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.IVipClientDepositCardVoService;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-06
 */
@Service
public class VipClientDepositCardVoServiceImpl extends ServiceImpl<VipClientDepositCardVoMapper, VipClientDepositCardVo> implements IVipClientDepositCardVoService {

	@Override
	public ResultInfo getOneVip(Integer vipId) {
		QueryWrapper<VipClientDepositCardVo> vipWrapper = new QueryWrapper<VipClientDepositCardVo>();
		vipWrapper.eq("vip_id",vipId);
		vipWrapper.select("vip_id",
							"client_name",
							"sex",
							"age",
							"reg_date",
							"deposit",
							"deposit_state",
							"card_name",
							"time_left",
							"vip_deadline",
							"phone",
							"vip_delete_flag");
		return ResultInfo.success(this.getOne(vipWrapper));
	}
	
	@Override
	public ResultInfo getListVip(
			Integer currentPage, 
			Integer pageSize, 
			String searchKey,
			String deadRange,
			String regRange) {
		// 构建查询条件
		QueryWrapper<VipClientDepositCardVo> vipWrapper = new QueryWrapper<VipClientDepositCardVo>();
		vipWrapper.select("vip_id","client_name","deposit_state","card_name","reg_date","vip_deadline","phone","vip_delete_flag");
		// 只显示没到会员deadline的用户
//		vipWrapper.ge("vip_deadline", LocalDateTime.now());
		// 如果searchKey不是空则加入模糊查询条件中
//		if (StringUtils.hasLength(searchKey)) {
//			vipWrapper.and(s -> s.like("client_name",searchKey)
//					.or().like("card_name",searchKey)
//					.or().like("deposit_state",searchKey)
//					.or().like("phone",searchKey));
//		}
		JSONArray searchArr = JSON.parseArray(searchKey);
		if (!searchArr.isEmpty()) {
			//验证查询会员名关键字是否有长度
			if (StringUtils.hasLength((String)searchArr.get(0))) {
				vipWrapper.like("client_name",searchArr.get(0));
			}
			//验证查询押金状态关键字是否有长度
			if (StringUtils.hasLength((String)searchArr.get(1))) {
				vipWrapper.like("deposit_state",searchArr.get(1));
			}
			//验证查询VIP类别关键字是否有长度
			if (StringUtils.hasLength((String)searchArr.get(2))) {
				vipWrapper.like("card_name",searchArr.get(2));
			}
			//验证查询联系电话关键字是否有长度
			if (StringUtils.hasLength((String)searchArr.get(3))) {
				vipWrapper.like("phone",searchArr.get(3));
			}
			//验证查询会员是否过期参数字符串是否为true
			if (searchArr.get(4) != null) {
				if((boolean) searchArr.get(4)) {
					vipWrapper.eq("vip_delete_flag", true);
				}
				if(!(boolean) searchArr.get(4)) {
					vipWrapper.eq("vip_delete_flag", false);
				}	
			}
		}
		JSONArray deadArr = JSON.parseArray(deadRange);
		System.out.println(deadArr);
		if (!deadArr.isEmpty()) {
			System.out.println("deadArr不为空");
			//会员到期时间大于查询起始时间
			vipWrapper.ge("vip_deadline", deadArr.get(0));
			//会员到期时间小于查询结束时间
			vipWrapper.le("vip_deadline", deadArr.get(1));
		}
		JSONArray regArr = JSON.parseArray(regRange);
		System.out.println(regArr);
		if (!regArr.isEmpty()) {
			System.out.println("regArr不为空");
			//会员到期时间大于查询起始时间
			vipWrapper.between("reg_date", regArr.get(0), regArr.get(1));
//			vipWrapper.ge("reg_date", regArr.get(0));
			//会员到期时间小于查询结束时间
//			vipWrapper.le("vip_deadline", regArr.get(1));
		}
		// 创建分页查询
		IPage<VipClientDepositCardVo> pageInfo = new Page<VipClientDepositCardVo>(currentPage, pageSize);
		// 返回查询结果
		return ResultInfo.success(this.page(pageInfo, vipWrapper));
	}


}
